package com.card.main;

import java.lang.reflect.Method;
import java.util.List;

import com.card.admin.pojo.AdminManager;
import com.card.game.JinUtils;
import com.card.match.MatchManager;
import com.card.setting.GameSetting;
import com.card.timer.GameTimerManager;
import com.xiaoao.net.global.Global;
import com.xiaoao.net.json.IMsgProcesser;
import com.xiaoao.net.json.JsonServer;
import com.xiaoao.net.main.AbstractMainApp;
import com.xiaoao.net.main.PackageUtil;
import com.xiaoao.net.main.ShutdownHook;
import com.xiaoao.net.rereader.ResReadUtil;


/**
 * 服务器主程序，入口
 *
 */
public class MainApp extends AbstractMainApp{

	@Override
	public void closeServer() {
        Global.logSysMessage("============关闭网络配置====================");
    	JsonServer.getInstance().serverExit();
        Global.logSysMessage("Server End");
        ShutdownHook.getInstance().setShutNormal(true);
    }
		
	@Override
    public void initServer() {
    	Global.logSysMessage("Server Start");
        Global.logSysMessage("============加载配置文件====================");
        ResReadUtil.init();
        Global.logSysMessage("============加载网络配置====================");
        JsonServer.getInstance().serverStart(new CardHanderProcessor());
        Global.logSysMessage("============初始化消息处理器====================");
        instanceInit();
        GameSetting.getInstance().init();
        AdminManager.getInstance().init();
        GameTimerManager.getInstance().startTimer();
        MatchManager.getInstance();
        Global.getInstance().setScreenShow(true);
        JinUtils.getAll2Cards(true);
//        MatchManager.getInstance().test();
    }
	
	private void instanceInit() {
        List<String> classNames = PackageUtil.getClassName("com.card");
        if (classNames != null) {
            try {
                for (String className : classNames) {
                    Class<?> temp = Class.forName(className);
                    if (!temp.equals(IMsgProcesser.class) && IMsgProcesser.class.isAssignableFrom(temp)) {
                        Method method = temp.getMethod("getInstance");
                        IMsgProcesser invoke = (IMsgProcesser) method.invoke(null);
                        invoke.registMsgId();
                    }
                }
            } catch (Exception ex) {
                Global.logErrorMessage(ex);
                System.exit(0);
            }
        }
    }
	
}
